import axios from "axios";

const server = axios.create({
    baseURL:'/api',
    timeout:600000
})

server.interceptors.request.use(config => {
    const token = sessionStorage.getItem('token')
    //如果token不为空，将token放入header中
    if (token) {
        config.headers["token"] = token
    }else{
        //否则提示token为空
        console.log("token为空")
    }
    return config
},error => {
    console.warn("请求拦截器失败",error)
    return Promise.reject(error)
})

//响应拦截器
server.interceptors.response.use(response => {
    if (response.data.code === 401){
        console.log("token为空")
    }
    // else if (response.data.code === 500){
    //     console.log("token错误")
    //     sessionStorage.removeItem('token')
    //     window.location.href = '/login'
    // }
    return response;
},error => {
    console.warn("响应拦截器失败",error)
    return Promise.reject(error)
})
export default server